﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Transactions;
using IssueTracker.Data;
using IssueTracker.Business.Entity;

namespace IssueTracker.Business.BO
{
    public class ProjectBO
    {
        private ProjectDataAccess da;

        public ProjectBO() 
        {
            da = new ProjectDataAccess();
        }


#region Business Methods

        /// <summary>
        /// Add a new project
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public Project AddNew(Project project)
        {
            Project returnProject;

            project.CreationDate = DateTime.Today.Date;
            

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnProject = da.Create(project);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }

            return returnProject;

        }

        public Project UpdateProject(Project project)
        {
            Project returnProject;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnProject = da.Update(project);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                
                return returnProject;
            }
        }

        public void DeleteProject(Project project)
        {
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    da.Delete(project);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<Project> ListProjects()
        {
            ProjectDataAccess data = new ProjectDataAccess();

            IEnumerable<Project> returnList = data.List();
            return returnList;
        }

        public Project GetProject(int id)
        {
            return da.Retrieve(id);
        }
        
#endregion

    }
}
